package com.amat0urheu.util;





import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.core.DockerClientBuilder;


public class DockerApiUtil {

    private static DockerClient dockerClient = createClient();

    private static DockerClient createClient() {

        try (DockerClient docker = DockerClientBuilder.getInstance("tcp://192.168.103.159:2375").build()) {
            String imageName = "ctftraining/qwb_2019_supersqli";

            // Pull 镜像
            docker.pullImageCmd(imageName).exec(new PullImageResultCallback()).awaitCompletion();

            System.out.println("成功拉取镜像：" + imageName);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  dockerClient;
    }

    public static void updateClient() {
        dockerClient = createClient();
    }

    public static void info() {

       createClient();
    }
//
//    public static List<SearchItem> searchDocker(String keyword) {
//        return dockerClient.searchImagesCmd(keyword).exec();
//    }
//
//    public static void pullImage(String imageName, PullImageResultCallback pullImageResultCallback) throws InterruptedException {
//        System.out.println(imageName);
//        dockerClient.pullImageCmd(imageName).exec(pullImageResultCallback);
//
//
//    }
//
//    public static void removeImage(String imageName) {
//        dockerClient.removeImageCmd(imageName)
//                .withForce(true)
//                .exec();
//    }
//
//    public static CreateContainerResponse createContainer(DockerContainer dockerContainer) {
//        PortBinding portBinding = PortBinding.parse(dockerContainer.getDstPort() + ":" + dockerContainer.getSrcPort());
//        HostConfig hostConfig = HostConfig.newHostConfig()
//                .withMemory(dockerContainer.getMemLimit() * 1024 * 1024L)
//                .withCpuCount((int)(dockerContainer.getCpuLimit()*100) * 10000000L)
//                .withPortBindings(portBinding);
//        CreateContainerResponse container = dockerClient.createContainerCmd(dockerContainer.getImageName())
//                .withName(dockerContainer.getName())
//                .withHostConfig(hostConfig)
//                .withEnv((dockerContainer.getFlag() != null)?String.format("%s=%s", "FLAG", dockerContainer.getFlag()):"FLAG=hubu_mars")
//                .withExposedPorts(ExposedPort.parse(dockerContainer.getSrcPort() + "/tcp"))
//                .exec();
//        return container;
//    }
//
//    public static void startContainer(String id) {
//        dockerClient.startContainerCmd(id).exec();
//    }
//
//    public static void stopContainer(String id) {
//        dockerClient.stopContainerCmd(id).exec();
//    }
//
//    public static void removeContainer(String id) {
//        dockerClient.removeContainerCmd(id)
//                .withForce(true)
//                .exec();
//    }
//
//    public static Boolean checkImage(String imageName) {
//        try {
//            InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageName).exec();
//            return true;
//        } catch (NotFoundException e) {
//            return false;
//        }
//
//    }
//
//    public static String[] resolveImageName(String imageName) {
//        String pattern = "(.*):(.*)";
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(imageName);
//        String[] res = new String[3];
//        while (m.find()) {
//            res[0] = imageName;
//            res[1] = m.group(1);
//            res[2] = m.group(2);
//            return res;
//        }
//        res[0] = imageName + ":latest";
//        res[1] = imageName;
//        res[2] = "latest";
//        return res;
//    }
//
//    public static void fun() {
//        dockerClient.pullImageCmd("hello-world").exec(new ResultCallback<PullResponseItem>() {
//            public void onStart(Closeable closeable) {
//                System.out.println("start");
//            }
//
//            public void onNext(PullResponseItem object) {
//
//                System.out.println("next" +object.getStatus());
//            }
//
//            public void onError(Throwable throwable) {
//                System.out.println("error");
//                throwable.printStackTrace();
//            }
//
//            public void onComplete() {
//                System.out.println("pull finished");
//            }
//
//            public void close() throws IOException {
//                System.out.println("close");
//            }
//        });
//    }


}
